import R from '@common/type/response';
import type { NextApiResponse } from 'next';
import * as roleService from '@/common/service/sys_role';
import { AuthenticatedRequest, withAuth } from '@common/middleware/auth';

export async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  const { id } = req.query;
  if (!id) {
    res.status(400).json({ success: false, message: '缺少角色ID' });
    return;
  }
  if (req.method === 'GET') {
    // 获取单个角色
    const data = await roleService.getRoleById(Number(id));
    R.success(res, data);
  } else if (req.method === 'PUT' || req.method === 'PATCH') {
    // 更新角色
    const affected = await roleService.updateRole(Number(id), req.body);
    R.success(res);
  } else if (req.method === 'DELETE') {
    // 删除角色
    const affected = await roleService.deleteRole(Number(id));
    R.success(res)
  } else {
    res.status(405).json({ success: false, message: 'Method Not Allowed' });
  }
}

export default withAuth(handler); 